#ifndef LB_MATRIXOPERATOR_H_
#define LB_MATRIXOPERATOR_H_

#include "LB_Matrix.h"

class Matrix {
public:
    Matrix()
    {
        printf("无参构造函数\n");
        this->mat=NULL;
    }
    Matrix(int rows,int cols)
    {
        this->mat=(LB_Matrix*)malloc(sizeof(LB_Matrix));
        lb_matrix_init_double(this->mat,rows,cols); 
        printf("构造函数\n");
    }
    Matrix(const Matrix& m)
    {
        this->mat=(LB_Matrix*)malloc(sizeof(LB_Matrix));
        this->mat->data=NULL; 
        m.mat->copy(this->mat,m.mat);
        printf("拷贝函数\n");
    }
    ~Matrix()
    {
        //printf("析构前\n");
        lb_matrix_free(this->mat);

        printf("析构函数\n");
    }
    void * element(int i,int j)
    {
        return this->mat->element(this->mat,i,j);
    }
    

    inline Matrix &operator=(const Matrix & a)
    {
        //printf("等好\n");
        if(this->mat==NULL)
        {
            printf("nihao\n");
            this->mat=(LB_Matrix*)malloc(sizeof(LB_Matrix));
            this->mat->data=NULL; 
        }
        //printf("dengho %d %d\n",a.mat->rows,a.mat->cols);

        a.mat->copy(this->mat,a.mat); 
        //printf("end copy\n");
        return *this;
    }
    LB_Matrix* mat;
};


static inline Matrix operator*(const Matrix&a,const Matrix&b)
{
    Matrix c(0,0);
    a.mat->copy(c.mat,a.mat);
    c.mat->mult(c.mat,b.mat);

    printf("乘法\n");
    return c;
}

static inline Matrix operator*(const Matrix&a,double fac)
{
    Matrix c(0,0);
    a.mat->copy(c.mat,a.mat);
    c.mat->scala_mult(c.mat,fac);
    return c;
}


static inline Matrix operator*(double fac,const Matrix&a)
{

    Matrix c(0,0);
    a.mat->copy(c.mat,a.mat);
    c.mat->scala_mult(c.mat,fac);

    return c;
}

static inline Matrix operator* (const Matrix& a ,void* p)
{
    Matrix c(0,0);
    a.mat->copy(c.mat,a.mat);
    c.mat->right_mult_array(c.mat,p);
    return c;
} 

static inline Matrix operator* (void* p,const Matrix& a)
{

    Matrix c(0,0);
    a.mat->copy(c.mat,a.mat);
    c.mat->left_mult_array(p,c.mat);
    return c;

}

static inline Matrix operator+(const Matrix& a,const Matrix& b)
{
    Matrix c(0,0);
    a.mat->copy(c.mat,a.mat);
    c.mat->plus(c.mat,b.mat);
    return c;
}

static inline Matrix operator-(const Matrix& a,const Matrix& b)
{
    Matrix c(0,0);
    a.mat->copy(c.mat,a.mat);
    c.mat->minus(c.mat,b.mat);
    return c;
}


#endif